# -*- coding: utf-8 -*-
# ----------------------------
# @Time    : 2022/5/29 12:06 PM
# @Author  : changqingai
# @FileName: 01-初识tf_data.py
# ----------------------------
import numpy as np
import tensorflow as tf
import pathlib
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as py

# 构建dataset
ds = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6, 7, 8])
print("ds:", ds)

# 转成numpy iterator对象，并遍历元素
data = ds.as_numpy_iterator()
print(data)
for ele in data:
    print("ele: ", ele)

# dataset的reduce方法
sum_value = ds.reduce(10, lambda state, value: state + value).numpy()
print("sum_value:", sum_value)

# 查看元素类型
print(ds.element_spec)

# dataset的指定shape生成
ds1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([4, 8]))
print(ds1.element_spec, list(ds1.as_numpy_iterator()))

ds2 = tf.data.Dataset.from_tensor_slices((tf.random.uniform([4]),
                                          tf.random.uniform([4, 4], maxval=10)))
print(ds2.element_spec, list(ds2.as_numpy_iterator()))

# dataset zip
ds3 = tf.data.Dataset.zip((ds1, ds2))
print(ds3.element_spec)
print(list(ds3.as_numpy_iterator()))

ds4 = tf.data.Dataset.from_tensors(tf.SparseTensor(indices=[[1, 1], [2, 2]],
                                                   values=[1, 2],
                                                   dense_shape=[3, 3]))
print(ds4.element_spec)

# convert numpy array to dataset
_, test = tf.keras.datasets.fashion_mnist.load_data()
test_images, test_label = test
ds5 = tf.data.Dataset.from_tensor_slices((test_images, test_label))
print(ds5.element_spec)
